package com.cnpinxi.service;

import com.cnpinxi.domain.Payment;
import com.cnpinxi.domain.User;
import com.cnpinxi.domain.UserAccount;
import com.cnpinxi.log.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-3-6.
 */
@Service
public class UserAccountServiceImpl implements UserAccountService {
    protected Logger logger = LogFactory.getLogger(getClass());
    private final static Integer CONCESSIONAL_PRICE = 5000 * 100;
    @Autowired
    UserService userService;
    @Autowired
    PaymentService paymentService;

    @Override
    public List<UserAccount> getUserAccountList(User user) {
    logger.info("getUserAccountList================>>>>");

        List<UserAccount> userAccountList = new ArrayList<>();
        //获取可兑换余额
        if (user.getUserType() == User.USER_TYPE_ORGANIZATION) {
            logger.info("单位用户，可以兑换！");
            UserAccount userAccount = new UserAccount();
            userAccount.setUserId(user.getTid());
            userAccount.setAccountType(UserAccount.ACCOUNT_TYPE_CONCESSIONAL);
            Map<String, String> map = new HashMap<>();
            map.put("userId", String.valueOf(user.getTid()));
            map.put("payMode", String.valueOf(Payment.PAY_MODE_CONVERSION));
            List<Payment> paymentList = paymentService.selectpaymentList(map);
            Integer payPrice = 0;
            for (Payment payment : paymentList
                    ) {
                payPrice += payment.getPrice();
            }
            if (payPrice < CONCESSIONAL_PRICE) {
                userAccount.setMoney(CONCESSIONAL_PRICE - payPrice);
                userAccountList.add(userAccount);
            }
        }
        return userAccountList;
    }
}
